package com.xlserver.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xlserver.dao.ConsultationMapper;
import com.xlserver.dao.LoginInfoMapper;
import com.xlserver.dao.SupervisorMapper;
import com.xlserver.dao.UserMapper;
import com.xlserver.domain.Consultation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 关于咨询的工具类
 */

@Component
public class TimeUtil {
    @Autowired
    ConsultationMapper consultationMapper;

    @Autowired
    LoginInfoMapper loginInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    SupervisorMapper supervisorMapper;

    /**
     * 获取今天日期
     */
    public String getNow(){
        // 当前时间
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        //获取今天的日期
        return sf.format(new Date());
    }
    /**
     * 获取今天日期+时间
     */
    public String getDateTime(){
        // 当前时间，HH为24小时制
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取今天的日期
        return sf.format(new Date());
    }
    /**
     * 今日咨询时长
     */
    public String timeOfConsultationToday(int cid) throws ParseException {
        QueryWrapper<Consultation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid).orderByDesc("h_beginTime");
        List<Consultation> consultations = consultationMapper.selectList(queryWrapper);
        Set<Consultation> todayConsultations = new HashSet<>();
        for (Consultation consultation : consultations) {
            String time = consultation.getBeginTime();
            if (getNow().equals(time.substring(0,time.indexOf(" ")))){
                todayConsultations.add(consultation);
            }
        }
        return getTimeDiff(todayConsultations);
    }
    /**
     * 获取单次咨询时长
     */
    public String getTimeDiff(Consultation consultation) throws ParseException{
        SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = mDateFormat.parse(consultation.getBeginTime());
        Date end = mDateFormat.parse(consultation.getEndTime());
        return parseTime(end.getTime() - begin.getTime());
    }
    /**
     * 获取今日咨询时长
     */
    public String getTimeDiff(Set<Consultation> consultations) throws ParseException {
        long diff = 0;
        for (Consultation consultation : consultations) {
            SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date begin = mDateFormat.parse(consultation.getBeginTime());
            Date end = mDateFormat.parse(consultation.getEndTime());
            // 这样得到的差值是微秒级别
            diff += (end.getTime() - begin.getTime());
        }
        return parseTime(diff);
    }
    /**
     * 将时长转换为hh:mm:ss的格式
     */
    private String parseTime(long diff){
        StringBuilder sb = new StringBuilder();
        long hour = diff / (1000 * 60 * 60);
        diff = diff % (1000 * 60 * 60);
        if (hour > 0) {
            if (hour>=10){
                sb.append(hour).append(":");
            }
            else {
                sb.append("0").append(hour).append(":");
            }
        }
        else {
            sb.append("00").append(":");
        }
        // 获取分
        long min = diff / (1000 * 60);
        diff = diff % (1000 * 60);
        if (min > 0) {
            if (min>=10){
                sb.append(min).append(":");
            }
            else {
                sb.append("0").append(min).append(":");
            }
        }
        else {
            sb.append("00").append(":");
        }
        // 获取秒
        long sec = diff / 1000;
        if (sec > 0) {
            if (sec>=10){
                sb.append(sec);
            }
            else {
                sb.append("0").append(sec);
            }

        }
        else {
            sb.append("00");
        }
        return sb.toString();
    }
    /**
     *今日咨询数
     */
    public int sumOfConsultationToday(int cid){
        QueryWrapper<Consultation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid).orderByDesc("h_beginTime");
        List<Consultation> consultations = consultationMapper.selectList(queryWrapper);
        int sum=0;
        for (Consultation consultation : consultations) {
            String time = consultation.getBeginTime();
            if (getNow().equals(time.substring(0,time.indexOf(" ")))){
                sum++;
            }
        }
        return sum;
    }
    /**
     *累计完成咨询数
     */
    public long sumOfConsultation(int cid){
        QueryWrapper<Consultation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        return consultationMapper.selectCount(queryWrapper);
    }

    /**
     * 获取评级double，即所有会话的平均评级
     */
    public double averageRank(List<Consultation> consultations){
        return consultations.stream().
                mapToInt(Consultation::getCrank).
                average().
                orElse(0);
    }
    /**
     *判断被咨询者是否空闲，空闲返回true
     */
    public boolean isFree(int id){
        QueryWrapper<Consultation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",id);
        queryWrapper.orderByDesc("h_beginTime");
        List<Consultation> res = consultationMapper.selectList(queryWrapper);
        return res.size() == 0 || !res.get(0).getBeginTime().equals(res.get(0).getEndTime());
    }
    /**
     * 返回当前月第一天(res[0])与最后一天(res[1])
     */
    public String[] getFirstAndLastDayOfMonth(){
        String[] res = new String[2];
        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 获取前月的第一天
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        res[0] = format.format(cale.getTime());
        // 获取前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        res[1] = format.format(cale.getTime());
        return res;
    }


}
